<?php
final class Db_Mysql_Mysqli extends Db_Mysql
{
    private static $instance = null;

    public static function getInstance()
    {
        return self::$instance ? self::$instance : (self::$instance = new self);
    }

    protected function connect()
    {
        if (!$this->connection)
        {
            $this->connection = new mysqli(
                $this->config()->getHost(),
                $this->config()->getUser(),
                $this->config()->getPassword(),
                $this->config()->getDatabase()
            );
        }
    }

    protected function disconnect()
    {
        $this->getConnection()->close();
    }

    protected function isConnected()
    {
        return (0 === $this->getConnection()->connect_errno);
    }

    /**
     *
     * @param string $query
     * @return mysqli_result
     */
    public function query($query)
    {
        return $this->getConnection()->query($query, MYSQLI_STORE_RESULT);
    }

    /**
     *
     * @param string $string
     * @param string $quoteChar
     * @return string
     */
    public function quote($string, $quoteChar = '\'')
    {
        return $quoteChar . $this->getConnection()->escape_string($string) . $quoteChar;
    }

    public function getConnectionError()
    {
        return $this->getConnection()->connect_errno;
    }
}